﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lion.WeiXin.Tools
{
    /// <summary>
    /// 字符串处理工具类
    /// </summary>
   public class StringHelper
    {
       /// <summary>
       /// 替换web 特殊字符
       /// </summary>
       /// <param name="beforeString">要替换的字符串</param>
       /// <returns></returns>
       public static string ReplaceWebStr(string beforeString)
       {
           string result = beforeString;
           string[] webStrs = { "<", ">", "/","\\", "'", "\""," ","{","}" };
           foreach (string item in webStrs)
           {
               result = result.Replace(item, "");
           }
           return result;
       }

       /// <summary>
       /// 得到一个随机唯一ID，格式：yyyyMMddHHmmss-(5位长度随机数)
       /// </summary>
       /// <param name="len">随机尾数长度</param>
       /// <returns></returns>
       public static string GetUniqueID(int length=6)
       {
           Random rd = new Random(Guid.NewGuid().GetHashCode());
           //自动产生订单编号，时间+随机数
           string item = "";
           for (int i = 0; i < length; i++)
           {
               if (i == 0)
               {
                   item += rd.Next(1, 10);
               }
               else {
                   item += rd.Next(0, 10);
               }
               
           }
           return DateTime.Now.ToString("yyyyMMddHHmmss") + item;
       }

       /// <summary>
       /// 获取订单编号：UserID（后三位）+yyMMdd++随机数：
       /// </summary>
       /// <param name="UserID">自动截取后三位</param>
       /// <param name="len"></param>
       /// <returns></returns>
       public static string CreateNewOrderId(string UserID, int length = 3)
       {
           Random rd = new Random(Guid.NewGuid().GetHashCode());
           //自动产生订单编号，时间+随机数
           string item = "";
           for (int i = 0; i < length; i++)
           {
               if (i == 0)
               {
                   item += rd.Next(1, 10);
               }
               else
               {
                   item += rd.Next(0, 10);
               }

           }
           if (UserID.Length > 3)
           {
               UserID = UserID.Substring(UserID.Length - 3, 3);
           }
           return string.Format("{0}{1}{2}",UserID, DateTime.Now.ToString("yyMMdd"),item);
       }

       /// <summary>
       /// 返回指定长度随机数字符串 
       /// </summary>
       /// <param name="length"></param>
       /// <returns></returns>
       public static string GetRandNumber(int length = 5)
       {
           Random rd = new Random(Guid.NewGuid().GetHashCode());
           //自动产生订单编号，时间+随机数
           string item = "";
           for (int i = 0; i < length; i++)
           {
               if (i == 0)
               {
                   item += rd.Next(1, 10);
               }
               else
               {
                   item += rd.Next(0, 10);
               }
           }
           return item;
       }
    }
}
